﻿namespace CS.Beehive
{
    /// <summary>
    /// Tcp相关工厂
    /// </summary>
    public static class TcpFactory
    {

        #region 相关静态统计属性

        /// <summary>
        /// 默认配置
        /// </summary>
        public static TcpServerSetting DefaultSetting;

        #endregion

        #region 创建TcpServer或TcpClient相关对象
        
        static TcpFactory()
        {
            DefaultSetting = new DefaultTcpServerSetting();
        }


        #region 创建TcpServer

        /// <summary>
        /// 创建Tcp服务
        /// <remarks>使用上次初始化的设定</remarks>
        /// </summary>
        /// <returns></returns>
        public static TcpServer CreateTcpServer()
        {
            var server = new TcpServer(DefaultSetting);
            return server;
        }

        /// <summary>
        /// 创建Tcp服务
        /// <remarks>使用参数初始化相关配置，缺省参数使用上次设定的配置</remarks>
        /// </summary>
        /// <returns></returns>
        public static TcpServer CreateTcpServer(int collections)
        {
            return CreateTcpServer(collections, DefaultSetting.SameIpMaxConnections, DefaultSetting.BufferSize, DefaultSetting.ReadWritePoolSize);
        }

        /// <summary>
        /// 创建Tcp服务
        /// <remarks>使用参数初始化相关配置，缺省参数使用上次设定的配置</remarks>
        /// </summary>
        /// <returns></returns>
        public static TcpServer CreateTcpServer(int collections, int sameIpCollections)
        {
            return CreateTcpServer(collections, sameIpCollections, DefaultSetting.BufferSize, DefaultSetting.ReadWritePoolSize);
        }


        /// <summary>
        /// 创建Tcp服务
        /// <remarks>使用参数初始化相关配置，缺省参数使用上次设定的配置</remarks>
        /// </summary>
        /// <returns></returns>
        public static TcpServer CreateTcpServer(int collections, int sameIpCollections, int bufferSize)
        {
            return CreateTcpServer(collections, sameIpCollections, bufferSize, DefaultSetting.ReadWritePoolSize);
        }

        /// <summary>
        /// 创建Tcp服务
        /// <remarks>使用参数初始化相关配置</remarks>
        /// </summary>
        /// <returns></returns>
        public static TcpServer CreateTcpServer(int collections, int sameIpCollections, int bufferSize, int poolSize)
        {
            var set = new DefaultTcpServerSetting
            {
                MaxConnections = collections,
                SameIpMaxConnections = sameIpCollections,
                BufferSize = bufferSize,
                ReadWritePoolSize = poolSize
            };
            var server = new TcpServer(set);
            return server;
        }

        #endregion


        #endregion



    }
}